var layer = layui.layer;

function delRow(obj) {
    var row = obj.parentNode.parentNode;
    var tds = row.getElementsByTagName("td");
    var iccid = tds[6].innerHTML;
    console.log(iccid);
    $.ajax({
        url: '/ticket/delete',
        method: 'POST',
        dataType: "json",
        traditional: true,
        data: {
            iccid: iccid
        },
        success: function (data) {
            if (data.status == 'ok') {
                layer.msg(data.msg);
                obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);//删除行
            } else {
                layer.msg(data.msg);
            }
        },
        error: function (data) {
        }
    });
}

function getRow(obj) {
    var row = obj.parentNode.parentNode;
    var tds = row.getElementsByTagName("td");
    var inputs = document.getElementById("editform").getElementsByTagName("input");
    inputs[1].value = tds[0].innerHTML.trim();
    inputs[4].value = tds[2].innerHTML;
    inputs[5].value = tds[3].innerHTML;
    inputs[6].value = tds[4].innerHTML;
    inputs[7].value = tds[5].innerHTML;
    inputs[8].value = tds[6].innerHTML;
    if (tds[1].innerHTML == '女') {
        inputs[3].checked = true;
    } else {
        inputs[2].checked = true;
    }
}

function bindTable(data) {
    var b1 = "<td><button class=\"btn btn-sm btn-warning updateclass\" onclick=\"getRow(this);\" data-toggle=\"modal\"\n" +
        "                        data-target=\"#myModal\">修&nbsp;&nbsp;改\n" +
        "                </button></td>";
    var b2 = " <td><button class=\"btn btn-sm btn-danger deleteclass\" onclick=\"delRow(this);\">删&nbsp;&nbsp;除</button><td>";
    $("#listTable  tr:not(:first)").empty("");
    $.each(data, function (i, items) {  //遍历获取数据
        var $tr = $("<tr></tr>"); //表格的一行，在里面可以加各种属性;
        var $td = $("<td></td>"); //行中的元素
        $tr.append($td.clone().text(items.bookname)); //把数据填充到单元格中
        $tr.append($td.clone().text(items.booksex));
        $tr.append($td.clone().text(items.startcity));
        $tr.append($td.clone().text(items.endcity));
        $tr.append($td.clone().text(items.startdate));
        $tr.append($td.clone().text(items.idcard));
        $tr.append($td.clone().text(items.iccid));
        $tr.html($tr.html() + b1);
        $tr.html($tr.html() + b2);
        $("#listTable").append($tr);//最后要把内容放入要绑定的地方
    });
}

function loadTicket() {
    $.ajax({
        url: 'ticket',
        method: 'get',
        dataType: "json",
        traditional: true,
        data: {},
        success: function (data) {
            if (data.status == 'ok') {
                layer.msg(data.msg);
                bindTable(data.tickets);
            } else {
                layer.msg(data.msg);
                $("#listTable  tr:not(:first)").empty("");
            }
        },
        error: function (data) {
        }
    })
}

function checklogin() {
    $.ajax({
        url: 'login/check',
        method: 'get',
        dataType: "json",
        traditional: true,
        data: {
        },
        success: function (data) {
            if(data.status == 'ok'){
                $('#mainhead').text("欢迎您。。。"+ data.name);
            }else{
                console.log('fail');
                $('#mainhead').html("您未登录请      <a href='/login.html'>登陆</a>");
            }
        },
        error: function (data) {

        }
    })
}